package com.molichuxing.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.molichuxing.entity.CarCacheDto;
import com.molichuxing.framework.utils.RedisUtil;
import com.molichuxing.service.CarJobService;
import com.molichuxing.services.business.dto.response.CarCheckDto;
import com.molichuxing.services.infrastructure.dto.response.CarDto;
import com.molichuxing.services.infrastructure.dto.response.CheckDto;
import com.molichuxing.services.infrastructure.service.CarService;
import com.molichuxing.services.infrastructure.service.YearlyInspectService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static com.molichuxing.exception.StatisticsRedisKey.CAR_INFO;

/**
 * 车辆任务service
 *
 * @author zhaopenghui
 * @date 2019年12月24日
 */
@Service("carJobService")
public class CarJobServiceImpl implements CarJobService {
    private static final Logger logger = LoggerFactory.getLogger(CarJobServiceImpl.class);
    @Resource
    private RedisUtil redisUtil;

    @Resource
    private CarService carService;

    @Resource
    private YearlyInspectService yearlyInspectService;

    /**
     * 缓存所有车辆
     *
     * @return
     * @throws Exception
     */
    @Override
    public Boolean cacheAllCars() {
        try {
            List<CarDto> carDtoList = carService.getCarsByBusinessTypeList(null);
            if (carDtoList != null && carDtoList.size() > 0) {
                List<CarCacheDto> carCacheDtoList = toCarCacheDtoList(carDtoList);

                Map<String, String> studentMap = carCacheDtoList.stream().collect(Collectors.toMap(CarCacheDto::getVin, a -> JSONObject.toJSONString(a), (k1, k2) -> k1));
                redisUtil.hmset(CAR_INFO, studentMap);
            }
        } catch (Exception e) {
            logger.error("[cacheAllCars]缓存所有车辆异常", e);
            return false;
        }
        return true;
    }


    private static List<CarCacheDto> toCarCacheDtoList(List<CarDto> beanList) {
        List<CarCacheDto> dtoList = null;
        if (beanList != null && beanList.size() > 0) {
            dtoList = new ArrayList<>();
            for (CarDto bean : beanList) {
                dtoList.add(toCarCacheDto(bean));
            }
        }

        return dtoList;
    }

    private static CarCacheDto toCarCacheDto(CarDto bean) {
        CarCacheDto beanDto = null;
        if (bean != null) {
            beanDto = new CarCacheDto();
            BeanUtils.copyProperties(bean, beanDto);
        }
        return beanDto;
    }


    @Override
    public Boolean modifyYearlyInspectionHandleStatus() {
        try {
            yearlyInspectService.modifyYearlyInspectionHandleStatus();
        } catch (Exception e) {
            logger.error("[modifyYearlyInspectionHandleStatus]修改年检状态异常", e);
            return false;
        }
        return true;
    }

}
